package com.net.xpay.stable;

import com.net.xpay.stable.job.SafePayBatchUpdateJob;
import com.net.xpay.common.helper.CommonSysConfigHelper;
import com.net.xpay.common.helper.NoticeHelper;
import com.net.xpay.core.manager.safePay.BookAccountChargeOrderManager;
import com.net.xpay.core.manager.safePay.SafePayBatchManager;
import com.net.xpay.core.manager.safePay.SafePayOrderManager;
import com.net.xpay.core.service.safePay.SafePayService;
import com.net.xpay.stable.job.BookAccountChargeOrderUpdateJob;
import com.net.xpay.stable.job.SafePayBatchBankTransUpdateJob;
import com.net.xpay.stable.job.SafePayOrderUpdateJob;
import com.net.shard.ShardDataSource;
import com.net.shard.ShardDataSourceConfigurer;
import com.net.shard.domain.ShardConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 后台任务的入口 方便注入service
 * @Description: <br>
 * @Project: hades <br>
 * @ Created in 2018/8/31 13:35 <br>
 */
@Service
public class JobEntry {

    @Autowired
    @Qualifier("shardDataSource")
    private DataSource shardDataSource;

    @Autowired
    private SafePayBatchManager safePayBatchManager;

    @Autowired
    private SafePayOrderManager safePayOrderManager;

    @Autowired
    private SafePayService safePayService;

    @Autowired
    private BookAccountChargeOrderManager bookAccountChargeOrderManager;

    @Autowired
    private CommonSysConfigHelper commonSysConfigHelper;

    @Autowired
    private NoticeHelper noticeHelper;

    public void start() {
        ShardDataSourceConfigurer shardDataSourceConfigurer = ((ShardDataSource) shardDataSource).getShardDataSourceConfigurer();
        Map<Long, ShardConfig> shardConfigMap = shardDataSourceConfigurer.getShardConfigMap();
        for (Long shardId : shardConfigMap.keySet()) {
            //安全付发起转账
            startSafePayBatchUpdateJob(shardId);
            //资金账本订单充值查询
            startBookAccountChargeOrderUpdateJob(shardId);
            //安全付银行转账批次查询
            startSafePayBatchBankTransUpdateJob(shardId);
            //安全付银行单笔转账状态跟新
            startSafePayOrderUpdateJob(shardId);
        }
    }

    private void startSafePayOrderUpdateJob(Long shardId) {
        SafePayOrderUpdateJob job = new SafePayOrderUpdateJob(shardId, safePayOrderManager, safePayService, commonSysConfigHelper, noticeHelper);
        Thread jobThread = new Thread(job);
        jobThread.setName("safePayOrderUpdateJob-" + shardId);
        jobThread.start();
    }

    private void startSafePayBatchBankTransUpdateJob(Long shardId) {
        SafePayBatchBankTransUpdateJob job = new SafePayBatchBankTransUpdateJob(shardId, safePayBatchManager, safePayService, commonSysConfigHelper, noticeHelper);
        Thread jobThread = new Thread(job);
        jobThread.setName("safePayBatchBankTransUpdateJob-" + shardId);
        jobThread.start();
    }

    private void startBookAccountChargeOrderUpdateJob(Long shardId) {
        BookAccountChargeOrderUpdateJob bookAccountChargeOrderUpdateJob = new BookAccountChargeOrderUpdateJob(shardId, bookAccountChargeOrderManager, safePayService);
        Thread bookAccountChargeOrderUpdateJobThread = new Thread(bookAccountChargeOrderUpdateJob);
        bookAccountChargeOrderUpdateJobThread.setName("bookAccountChargeOrderUpdateJob-" + shardId);
        bookAccountChargeOrderUpdateJobThread.start();
    }

    private void startSafePayBatchUpdateJob(Long shardId) {
        SafePayBatchUpdateJob job = new SafePayBatchUpdateJob(shardId, safePayBatchManager, safePayService, commonSysConfigHelper, noticeHelper);
        Thread jobThread = new Thread(job);
        jobThread.setName("safePayBatchUpdateJob-" + shardId);
        jobThread.start();
    }
}
